//
//  GenericDownloader.h
//  Catalogo
//
//  Created by UJI on 22/05/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GenericDownloadItem.h"

@protocol GenericDownloaderDelegate

- (void) genericDownloaderStarted: (int) numItemsToDownload;
- (void) genericDownloaderProcessChanged: (int) numItemsDownloaded;
- (void) genericDownloaderItemDownloadSuccess: (GenericDownloadItem *) downloadItem;
- (void) genericDownloaderItemDownloadError: (GenericDownloadItem *) downloadItem;
- (void) genericDownloaderFinished;
- (void) genericDownloaderFinishedWithNumErrors: (int) numErrors andNumItemsDownloaded: (int) numItemsDownloaded;
- (void) genericDownloaderError: (NSString *) message;
- (void) genericDownloaderStopped;

@optional
- (void) genericDownloaderProgressStarted: (long) totalSize;
- (void) genericDownloaderProgressChanged: (long) downloadedSize totalSize: (long) totalSize;
@end

@interface GenericDownloader : NSObject {
    NSObject<GenericDownloaderDelegate> * downloadDelegate;
    NSOperationQueue * queue;
    NSFileManager * fileManager;
    int numDownloads;
    int numDownloadedFiles;
    
    NSMutableDictionary * retriesHash;
    int numRetries;
    BOOL errorSent;
    BOOL stopped;
    
    //En caso de ser cierto si se produce un error no se para la descarga y en lugar de terminar con un genericDownloaderFinished se
    //termina con un genericDownloaderFinishedWithNumErrors
    BOOL allowFinishWithErrors;
    int numDownloadErrors;
    
    //Si se va a calcular el tamaño de descarga y el progreso va a ser accurate
    BOOL useAccurateProgress;
    long totalSize;
    long totalDownload;
}
- (void) startDownload;
- (void) stopDownload;
- (void) startDownloadItem: (GenericDownloadItem *) downloadItem;
- (void) onDownloadItemError: (GenericDownloadItem *) downloadItem;
- (void) calculateTotalSize;

@property (nonatomic, retain) NSObject<GenericDownloaderDelegate> * downloadDelegate;
@property (nonatomic, retain) NSArray * downloadItems;
@property (nonatomic, assign) int numRetries;
@property (nonatomic, assign) BOOL allowFinishWithErrors;

@end
